package com.boco.modules.fdoc.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.boco.common.utils.JsonUtils;
import com.boco.modules.fdoc.dao.HosPerformanceDao;
import com.boco.modules.fdoc.service.HosPerformanceService;
import com.boco.modules.fdoc.vo.HosPerformanceVo;

import java.text.DecimalFormat;
@Service
public class HosPerformanceServiceImpl implements HosPerformanceService {

	@Resource
	HosPerformanceDao hosPerformanceDao;
	
	@Override
	public String querySigPack() {
		List<HosPerformanceVo> packs = hosPerformanceDao.querySigPack();
		String json = JsonUtils.getJson(packs);
		return json;
	}

	@Override
	public HosPerformanceVo queryPerformance() {
		HosPerformanceVo vo = new HosPerformanceVo();
		List<HosPerformanceVo> hosList = hosPerformanceDao.queryHosPerformance();
		double num1=0,num2=0,num3=0;
		String name1="";
		String name2="";
		String name3="";
		DecimalFormat dcmFmt = new DecimalFormat("0.00");
		for (HosPerformanceVo hos : hosList) {
			List<HosPerformanceVo> packList = hosPerformanceDao.querySigInfo(hos.getHosId());
			int count1 = 0,count2 = 0;
			for (HosPerformanceVo pack : packList) {
				count1 = count1 + pack.getNum()*pack.getCount();
				count2 = count2 + pack.getNum()*pack.getItem();
			}
			hos.setSerCount(count1);//当前医院的总的应服务次数
			hos.setItems(count2);//当前医院总的服务项目数
			String progress1 = dcmFmt.format(Double.valueOf(hos.getFullCount())/hos.getSerCount()*100) + "%";//完成率进度
			String progress2 = dcmFmt.format(Double.valueOf(hos.getTimeCount())/hos.getSerCount()*100) + "%";//及时性进度
			String progress3 = dcmFmt.format(Double.valueOf(hos.getFullItem())/hos.getItems()*100) + "%";//缺项率进度
			hos.setProgress1(progress1);
			hos.setProgress2(progress2);
			hos.setProgress3(progress3);
			hos.setList(packList);
			num1 += Double.valueOf(progress1.substring(0, progress1.length()-1));
			num2 += Double.valueOf(progress2.substring(0, progress2.length()-1));
			num3 += Double.valueOf(progress3.substring(0, progress3.length()-1));
		}
		num1 = num1/hosList.size();
		num2 = num2/hosList.size();
		num3 = num3/hosList.size();
		double max,in,min; 
		if(num1>num2){ 
			max=num1;min=num2; 
			name1="完成率";name3="及时性";
		}else{ 
			max=num2;min=num1; 
			name1="及时性";name3="完成率";
		} 
		if(num3>max){ 
			in=max; max=num3; 
			name2=name1;name1="缺项率";
		}else if(num3>min){ 
			in=num3;name2="缺项率";
		}else{
			in=min;name2=name3;
			min=num3;name3="缺项率";
		}
		
		vo.setNum1(dcmFmt.format(max));
		vo.setNum2(dcmFmt.format(in));
		vo.setNum3(dcmFmt.format(min));
		vo.setName1(name1);
		vo.setName2(name2);
		vo.setName3(name3);
		vo.setPerformance(hosList);
		return vo;
	}

	@Override
	public String queryResident(HosPerformanceVo vo) {
		List<HosPerformanceVo> resident = hosPerformanceDao.queryResident(vo);
		for (HosPerformanceVo r : resident) {
			if ("0".equals(r.getSex())||"男".equals(r.getSex())) {
				r.setSex("男");
			}else{
				r.setSex("女");
			}
		}
		String json = JsonUtils.getJson(resident);
		return json;
	}

}
